在 C++ 中,從抽象值到執行的旅程,始於 語句。一個 表示式語句 僅需在表示式後加上分號即可建立,強制編譯器進行評估,並順序地推進 控制流程 順序執行。
1. 空語句
一個 空語句 (;) 是當語言要求語句但你的邏輯不需要時所使用的占位符。雖然在某些迴圈中很有用,但請注意 額外的空語句——在 while 或 if 語句頭部意外加上分號,可能導致嚴重的邏輯錯誤,使預期的程式碼區塊被忽略。
⚠️ 警告(第 235 頁): 迴圈頭部後意外加上分號,會將空語句當作迴圈內容,常導致無限迴圈。
2. 複合語句(區塊)
一個 複合語句,或 區塊,是由大括號「{ }」包圍的一連串語句。它被視為一個執行單元。區塊具有自己的作用域;內部定義的名稱在外部不可見。 { }。它被視為一個執行單位。區塊具有自己的作用域;內部定義的名稱在外部不可見。
註解(第 235 頁): 與簡單語句不同,區塊 不 不會以分號結尾。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which of the following creates an 'expression statement'?
An expression followed by a semicolon.
Any line of code within curly braces.
A variable declaration without an assignment.
A comment followed by a semicolon.
✅ Correct!
Correct. In C++, adding a semicolon to an expression turns it into a statement.❌ Incorrect
An expression statement specifically refers to an expression terminated by a semicolon.QUESTION 2
Exercise 5.2: What is a block and when might you use one?
A block is a group of functions; used to organize large projects.
A block is a single semicolon; used to end a program.
A block is a sequence of statements in braces; used where the syntax requires one statement but logic requires many.
A block is a reserved memory segment; used for global variables.
✅ Correct!
Precisely. A block (compound statement) allows multiple statements to be treated as one, which is essential for control structures like 'if' and 'while'.❌ Incorrect
Recall that a block is defined by { } and acts as a single execution unit.QUESTION 3
What is the primary danger of an 'extraneous null statement' after a while loop header?
The compiler will throw a syntax error.
The loop body will be treated as the null statement, causing the intended logic to execute only once or never.
It automatically converts the loop into a do-while loop.
It speeds up the execution by skipping checks.
✅ Correct!
Correct. If you write 'while(cond); { body }', the semicolon IS the body, and the block in braces is just a separate statement executed after the loop finishes.❌ Incorrect
The compiler sees it as valid syntax, but it changes the logic entirely—the semicolon becomes the loop's body.QUESTION 4
Exercise 5.3: Does using the comma operator to compress a while loop into a single statement without a block improve readability?
Yes, less lines always means clearer code.
No, it typically diminishes readability by hiding the loop's intent and side effects.
Yes, it is the standard way to write C++ loops.
No, the comma operator cannot be used in while loops.
✅ Correct!
Correct. While syntactically valid, squeezing logic into the loop header using commas makes the code harder to debug and understand.❌ Incorrect
Readable code clearly separates condition checking from the actions taken. Comma operators can obscure this.QUESTION 5
Which statement about block termination is correct?
Every block must end with a semicolon after the closing brace.
A block is NOT terminated by a semicolon.
Only blocks inside functions require a semicolon.
A block must end with a null statement.
✅ Correct!
Correct. The closing brace itself signals the end of the compound statement.❌ Incorrect
Adding a semicolon after a closing brace creates an unnecessary null statement.Statement Logic & Factorials
Applying anatomy of statements to algorithmic logic.
You are tasked with implementing a factorial function and a vowel counter. You must ensure proper scoping and avoid syntax-induced logic errors.
Q
1. [Writing Task] Write your own version of the factorial function ('fact') using a while loop. (Difficulty: Medium, Length: 75 words)
Solution:
A robust version of factorial should use a 'long long' to prevent overflow:
A robust version of factorial should use a 'long long' to prevent overflow:
long long fact(int val) {
long long res = 1;
while (val > 1)
res *= val--; // Expression statement
return res;
}
This function uses a single expression statement as the loop body. A block isn't strictly required here, but could be added for clarity. We decrement 'val' within the expression to move toward the termination condition.Q
2. [Writing Task] Exercise 5.10: Implement a logic block that counts both lower- and uppercase vowels in a string using a switch statement.
Solution:
To count both cases, we stack case labels to share a single block of code:
To count both cases, we stack case labels to share a single block of code:
switch (ch) {
case 'a': case 'A':
++aCnt;
break;
case 'e': case 'E':
++eCnt;
break;
// ... repeat for i, o, u
}
By omitting 'break' between 'a' and 'A', the flow of control falls through, effectively counting both characters toward the same counter.